package nc.gk.gkxm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import nc.gk.gkxm.mapper.SortMapper;
import nc.gk.gkxm.model.dto.SortDto;
import nc.gk.gkxm.model.entity.Sort;
import nc.gk.gkxm.model.vo.SortVO;
import nc.gk.gkxm.model.vo.TopicVO;
import nc.gk.gkxm.service.SortService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SortServiceImpl extends ServiceImpl<SortMapper, Sort> implements SortService {
    @Resource
    private SortMapper sortMapper;

    @Override
    public List<TopicVO> getTopicsBySorttId(Integer sortId) {
        List<TopicVO> topList = null;
//        判断是否存在sortId,存在则查询，不存在则抛出异常
        if (sortId != null) {
            //查询
            topList = sortMapper.getTopicsBySortId(sortId);
        } else {
            //抛出异常
            throw new RuntimeException("sortId不存在");
        }
        return topList;
    }

    @Override
    public List<SortVO> getAllSort(SortDto sortDto) {
//        判断是否为空，如果为空就查询初始父类型0
        if (null == sortDto.getSortpid() || "".equals(sortDto.getSortpid())) {
            sortDto.setSortpid(0);
        }
//        查询不为空，但是不存在的父类型
        if (sortMapper.getSortByPid(sortDto.getSortpid()).size() == 0)
            throw new RuntimeException("不存在此类型:sort为" + sortDto.getSortpid());

//       查询对应的子类型
        return sortMapper.getSortByPid(sortDto.getSortpid());
    }

    @Override
    public void saveSoft(SortDto sortDto) {
        if (null == sortDto)
            throw new RuntimeException("数据异常");
        int i = sortMapper.saveSoft(sortDto);
        if (i == 0)
            throw new RuntimeException("保存失败");

    }

    @Override
    public void updateSort(SortDto sortDto) {
        System.out.println(sortDto);
        if(null==sortDto)
            throw new RuntimeException("数据异常");
        int i=sortMapper.updateSort(sortDto);
        if(i==0)
            throw new RuntimeException("修改失败");
    }
}
